博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
9 个超实用的 jQuery 代码片段
阅读量:7048 次
发布时间:2019-06-28

本文共 4824 字,大约阅读时间需要 16 分钟。

1.  jQuery平滑回到顶端效果 

Javascript代码

$(document).ready(function() {        $("a.topLink").click(function() {          $("html, body").animate({              scrollTop: $($(this).attr("href")).offset().top + "px"          }, {              duration: 500,              easing: "swing"          });          return false;      });    });

2.  jQuery处理图片尺寸 

Javascript代码

$(window).bind("load", function() {      // 图片修改大小      $('#imglist img').each(function() {          var maxWidth = 120;          var maxHeight = 120;          var ratio = 0;          var width = $(this).width();          var height = $(this).height();                if(width > maxWidth){              ratio = maxWidth / width;              $(this).css("width", maxWidth);              $(this).css("height", height * ratio);              height = height * ratio;          }                  if(height > maxHeight){              ratio = maxHeight / height;              $(this).css("height", maxHeight);              $(this).css("width", width * ratio);              width = width * ratio;          }      });    });

3.  jQuery实现的滚动自动加载代码 

Javascript代码

var loading = false;  $(window).scroll(function(){      if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){          if(loading == false){              loading = true;              $('#loadingbar').css("display","block");              $.get("load.php?start="+$('#loaded_max').val(), function(loaded){                  $('body').append(loaded);                  $('#loaded_max').val(parseInt($('#loaded_max').val())+50);                  $('#loadingbar').css("display","none");                  loading = false;              });          }      }  });    $(document).ready(function() {      $('#loaded_max').val(50);  });

4.  jQuery测试密码强度 

Javascript代码

$('#pass').keyup(function(e) {       var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*[        DISCUZ_CODE_372        ]quot;, "g");       var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*[        DISCUZ_CODE_372        ]quot;, "g");       var enoughRegex = new RegExp("(?=.{6,}).*", "g");       if (false == enoughRegex.test($(this).val())) {               $('#passstrength').html('More Characters');       } else if (strongRegex.test($(this).val())) {               $('#passstrength').className = 'ok';               $('#passstrength').html('Strong!');       } else if (mediumRegex.test($(this).val())) {               $('#passstrength').className = 'alert';               $('#passstrength').html('Medium!');       } else {               $('#passstrength').className = 'error';               $('#passstrength').html('Weak!');       }       return true;  });
 

5.  jQuery实现的DIv高度保持一致

var maxHeight = 0;    $("div").each(function(){     if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }  });    $("div").height(maxHeight);

6.  简单处理IE6上PNG格式文件

$('.pngfix').each( function() {     $(this).attr('writing-mode', 'tb-rl');     $(this).css('background-image', 'none');     $(this).css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="path/to/image.png",sizingMethod="scale")');  });

将class=pngfix添加到需要处理的对象即可。 

7.  jQuery处理JSON 
Javascript代码

function parseJson(){      //Start by calling the json object, I will be using a           //file from my own site for the tutorial       //Then we declare a callback function to process the data      $.getJSON('hcj.json',getPosts);         //The process function, I am going to get the title,           //url and excerpt for 5 latest posts      function getPosts(data){             //Start a for loop with a limit of 5           for(var i = 0; i < 5; i++){              //Build a template string of                           //the post title, url and excerpt              var strPost = '

' + data.posts[i].title + '

' + '

' + data.posts[i].excerpt + '

' + 'Read >'; //Append the body with the string $('body').append(strPost); } } } //Fire off the function in your document ready $(document).ready(function(){ parseJson(); });

8.  jQuery实现让整个div可以被点击 

Javascript代码

$(".myBox").click(function(){      window.location=$(this).find("a").attr("href");       return false; });

9.  jQuery实现的Facebook风格的图片预加载效果 

Javascript代码

var nextimage = "http://www.gbtags.com/gb/networks/uploadimgthumb/55d67b14-fb25-45e7-acc8-211a41047ef0.jpg";  $(document).ready(function(){    window.setTimeout(function(){      var img = $("").attr("src", nextimage).load(function(){       $('div').append(img);      });    }, 100);  });

转载于:https://www.cnblogs.com/onflying/p/3216892.html

你可能感兴趣的文章
Django 安装
查看>>
jQuery用unbind方法去掉hover事件及其他方法介绍
查看>>
Centos Git1.7.1升级到Git2.2.1
查看>>
linux修改PS1,自定义命令提示符样式
查看>>
ArcMap中,如何查看当前工具是否在执行?如何将工具调到前台来执行?
查看>>
算法题总结----数组(二分查找)
查看>>
OPENWRT make menuconfig错误之一
查看>>
Django框架简介-模型系统
查看>>
可集成到APP的车架号识别软件
查看>>
导出查询结果到csv文件
查看>>
Algs4-2.3.19五取样切分
查看>>
Numpy 数据类型和基本操作
查看>>
HanzFontMaker--支持所有字体的点阵取模软件
查看>>
IDEA常用快揵键
查看>>
git 学习笔记
查看>>
[HDU5528]Count a * b
查看>>
[HDU5968]异或密码
查看>>
Vue的安装
查看>>
iOS开发~CocoaPods使用详细说明
查看>>
书城项目第五阶段---book表的curd
查看>>